package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.RolesDAO;
import org.gap.dao.RolesTiersDAO;
import org.gap.dao.TiersDAO;

import org.gap.domain.Roles;
import org.gap.domain.RolesTiers;
import org.gap.domain.Tiers;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for RolesTiers entities
 * 
 */

@Service("RolesTiersService")
@Transactional
public class RolesTiersServiceImpl implements RolesTiersService {

	/**
	 * DAO injected by Spring that manages Roles entities
	 * 
	 */
	@Autowired
	private RolesDAO rolesDAO;

	/**
	 * DAO injected by Spring that manages RolesTiers entities
	 * 
	 */
	@Autowired
	private RolesTiersDAO rolesTiersDAO;

	/**
	 * DAO injected by Spring that manages Tiers entities
	 * 
	 */
	@Autowired
	private TiersDAO tiersDAO;

	/**
	 * Instantiates a new RolesTiersServiceImpl.
	 *
	 */
	public RolesTiersServiceImpl() {
	}

	/**
	 * Delete an existing RolesTiers entity
	 * 
	 */
	@Transactional
	public void deleteRolesTiers(RolesTiers rolestiers) {
		rolesTiersDAO.remove(rolestiers);
		rolesTiersDAO.flush();
	}

	/**
	 * Save an existing RolesTiers entity
	 * 
	 */
	@Transactional
	public void saveRolesTiers(RolesTiers rolestiers) {
		RolesTiers existingRolesTiers = rolesTiersDAO.findRolesTiersByPrimaryKey(rolestiers.getRotRole(), rolestiers.getRotTiers());

		if (existingRolesTiers != null) {
			if (existingRolesTiers != rolestiers) {
				existingRolesTiers.setRotRole(rolestiers.getRotRole());
				existingRolesTiers.setRotTiers(rolestiers.getRotTiers());
				existingRolesTiers.setRotDescriptif(rolestiers.getRotDescriptif());
				existingRolesTiers.setRotDateDebut(rolestiers.getRotDateDebut());
				existingRolesTiers.setRotDateFin(rolestiers.getRotDateFin());
			}
			rolestiers = rolesTiersDAO.store(existingRolesTiers);
		} else {
			rolestiers = rolesTiersDAO.store(rolestiers);
		}
		rolesTiersDAO.flush();
	}

	/**
	 * Delete an existing Roles entity
	 * 
	 */
	@Transactional
	public RolesTiers deleteRolesTiersRoles(String rolestiers_rotRole, Integer rolestiers_rotTiers, String related_roles_rolRole) {
		RolesTiers rolestiers = rolesTiersDAO.findRolesTiersByPrimaryKey(rolestiers_rotRole, rolestiers_rotTiers, -1, -1);
		Roles related_roles = rolesDAO.findRolesByPrimaryKey(related_roles_rolRole, -1, -1);

		rolestiers.setRoles(null);
		related_roles.getRolesTierses().remove(rolestiers);
		rolestiers = rolesTiersDAO.store(rolestiers);
		rolesTiersDAO.flush();

		related_roles = rolesDAO.store(related_roles);
		rolesDAO.flush();

		rolesDAO.remove(related_roles);
		rolesDAO.flush();

		return rolestiers;
	}

	/**
	 * Save an existing Tiers entity
	 * 
	 */
	@Transactional
	public RolesTiers saveRolesTiersTiers(String rotRole, Integer rotTiers, Tiers related_tiers) {
		RolesTiers rolestiers = rolesTiersDAO.findRolesTiersByPrimaryKey(rotRole, rotTiers, -1, -1);
		Tiers existingtiers = tiersDAO.findTiersByPrimaryKey(related_tiers.getTieTiers());

		// copy into the existing record to preserve existing relationships
		if (existingtiers != null) {
			existingtiers.setTieTiers(related_tiers.getTieTiers());
			existingtiers.setTieIdentClient(related_tiers.getTieIdentClient());
			existingtiers.setTieTypeTiers(related_tiers.getTieTypeTiers());
			existingtiers.setTieLibExterne(related_tiers.getTieLibExterne());
			existingtiers.setTieNationalite(related_tiers.getTieNationalite());
			existingtiers.setTieTelFix(related_tiers.getTieTelFix());
			existingtiers.setTieTelBureau(related_tiers.getTieTelBureau());
			existingtiers.setTieTelPortable(related_tiers.getTieTelPortable());
			existingtiers.setTieFax(related_tiers.getTieFax());
			existingtiers.setTieEMail(related_tiers.getTieEMail());
			existingtiers.setTieDateCreation(related_tiers.getTieDateCreation());
			existingtiers.setTieUserCreation(related_tiers.getTieUserCreation());
			related_tiers = existingtiers;
		}

		rolestiers.setTiers(related_tiers);
		related_tiers.getRolesTierses().add(rolestiers);
		rolestiers = rolesTiersDAO.store(rolestiers);
		rolesTiersDAO.flush();

		related_tiers = tiersDAO.store(related_tiers);
		tiersDAO.flush();

		return rolestiers;
	}

	/**
	 * Save an existing Roles entity
	 * 
	 */
	@Transactional
	public RolesTiers saveRolesTiersRoles(String rotRole, Integer rotTiers, Roles related_roles) {
		RolesTiers rolestiers = rolesTiersDAO.findRolesTiersByPrimaryKey(rotRole, rotTiers, -1, -1);
		Roles existingroles = rolesDAO.findRolesByPrimaryKey(related_roles.getRolRole());

		// copy into the existing record to preserve existing relationships
		if (existingroles != null) {
			existingroles.setRolRole(related_roles.getRolRole());
			existingroles.setRolDescription(related_roles.getRolDescription());
			existingroles.setRolPhysiqueMorale(related_roles.getRolPhysiqueMorale());
			related_roles = existingroles;
		}

		rolestiers.setRoles(related_roles);
		related_roles.getRolesTierses().add(rolestiers);
		rolestiers = rolesTiersDAO.store(rolestiers);
		rolesTiersDAO.flush();

		related_roles = rolesDAO.store(related_roles);
		rolesDAO.flush();

		return rolestiers;
	}

	/**
	 * Delete an existing Tiers entity
	 * 
	 */
	@Transactional
	public RolesTiers deleteRolesTiersTiers(String rolestiers_rotRole, Integer rolestiers_rotTiers, Integer related_tiers_tieTiers) {
		RolesTiers rolestiers = rolesTiersDAO.findRolesTiersByPrimaryKey(rolestiers_rotRole, rolestiers_rotTiers, -1, -1);
		Tiers related_tiers = tiersDAO.findTiersByPrimaryKey(related_tiers_tieTiers, -1, -1);

		rolestiers.setTiers(null);
		related_tiers.getRolesTierses().remove(rolestiers);
		rolestiers = rolesTiersDAO.store(rolestiers);
		rolesTiersDAO.flush();

		related_tiers = tiersDAO.store(related_tiers);
		tiersDAO.flush();

		tiersDAO.remove(related_tiers);
		tiersDAO.flush();

		return rolestiers;
	}

	/**
	 * Load an existing RolesTiers entity
	 * 
	 */
	@Transactional
	public Set<RolesTiers> loadRolesTierss() {
		return rolesTiersDAO.findAllRolesTierss();
	}

	/**
	 * Return all RolesTiers entity
	 * 
	 */
	@Transactional
	public List<RolesTiers> findAllRolesTierss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<RolesTiers>(rolesTiersDAO.findAllRolesTierss(startResult, maxRows));
	}

	/**
	 * Return a count of all RolesTiers entity
	 * 
	 */
	@Transactional
	public Integer countRolesTierss() {
		return ((Long) rolesTiersDAO.createQuerySingleResult("select count(*) from RolesTiers o").getSingleResult()).intValue();
	}

	/**
	 */
	@Transactional
	public RolesTiers findRolesTiersByPrimaryKey(String rotRole, Integer rotTiers) {
		return rolesTiersDAO.findRolesTiersByPrimaryKey(rotRole, rotTiers);
	}
}
